package com.anlogic.sdk.bsp.settings;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Tree;

public class ConfigView {
	BspSettingsView parentView;

	private IBspSettingTool bspSettingTool;

	private Composite content;

	private TreeViewer configViewer;

	private TreeViewerColumn[] configViewerColumn;

	private Map<String, Config> configMap;

	private Label titleLabel;

	private boolean createTitle;

	private String[] configHeaders = new String[] { "Name", "Value", "Default", "Type", "Description" };

	private int[] configHeaderWidths = new int[] { 200, 150, 150, 100, 350 };

	public ConfigView(BspSettingsView parentView, Composite parent, IBspSettingTool bspSettingTool) {
		this.parentView = parentView;
		this.bspSettingTool = bspSettingTool;
		this.createTitle = true;
		this.createContentArea(parent);
		this.configMap = new HashMap<String, Config>();
	}

	public void setTitle(String type, String text) {
		if (this.createTitle) {
			this.titleLabel.setText(String.format("Configuration for %s: %s", type, text));
		}
	}

	public void setConfigContentProvider(IStructuredContentProvider p) {
		this.configViewer.setContentProvider((IContentProvider) p);
	}

	public void setConfigLabelProvider(ITableLabelProvider p) {
		this.configViewer.setLabelProvider((IBaseLabelProvider) p);
	}

	public void setConfigEditingSupport(EditingSupport es) {
		this.configViewerColumn[1].setEditingSupport(es);
	}

	public void refreshConfig(Object input) {
		this.configViewer.setInput(input);
		this.configViewer.refresh();
	}

	public TreeViewer getConfigViewer() {
		return this.configViewer;
	}

	public void setCreateTitle(boolean createTitle) {
		this.createTitle = createTitle;
	}

	private void createContentArea(Composite parent) {
		this.content = new Composite(parent, SWT.NONE);
		this.content.setLayout((Layout) new GridLayout(1, false));
		this.content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		createTitle(this.content);

		createConfigView(this.content);
	}

	private void createTitle(Composite parent) {
		Composite c = new Composite(parent, SWT.NONE);
		c.setLayout((Layout) new GridLayout(1, false));
		c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		if (this.createTitle) {
			this.titleLabel = new Label(c, SWT.NONE);
			this.titleLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
			this.titleLabel.setText("Configuration for OS: standalone");
		}
	}

	private void createConfigView(Composite parent) {
		this.configViewer = new TreeViewer(parent, SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE);
		this.configViewer.setUseHashlookup(true);
		this.configViewerColumn = new TreeViewerColumn[this.configHeaders.length];
		for (int i = 0; i < this.configHeaders.length; i++) {
			this.configViewerColumn[i] = new TreeViewerColumn(this.configViewer, 4);
			this.configViewerColumn[i].getColumn().setText(this.configHeaders[i]);
			this.configViewerColumn[i].getColumn().setWidth(this.configHeaderWidths[i]);
			this.configViewerColumn[i].getColumn().setResizable(true);
			this.configViewerColumn[i].getColumn().setMoveable(false);
		}
		Tree tree = this.configViewer.getTree();
		tree.setHeaderVisible(true);
		tree.setLinesVisible(true);
		tree.setLayout((Layout) new GridLayout(1, false));
		GridData td = new GridData(SWT.FILL, SWT.FILL, true, true);
		td.horizontalIndent = 5;
		tree.setLayoutData(td);
	}

	public boolean addConfig(String component, String compType) {
		if (this.configMap.containsKey(component))
			return false;
		Config c = new Config(this.getConfigViewer(), component, compType, this.bspSettingTool);
		if (c.getProperties().isEmpty())
			return false;
		this.configMap.put(component, c);
		this.bspSettingTool.addProperty(component, compType, c.getProperties());
		return true;
	}

	public void removeConfig(String component, String compType) {
		if (this.configMap.containsKey(component)) {
			this.configMap.remove(component);
			this.bspSettingTool.removeProperty(component, compType);
		}
	}

	public void setCurrentConfig(String component) {
		if (!this.configMap.containsKey(component))
			return;
		Config c = this.configMap.get(component);
		this.setTitle(c.getType(), component);
		this.setConfigContentProvider((IStructuredContentProvider) c.getContentProvider());
		this.setConfigLabelProvider(c.getLabelProvider());
		this.setConfigEditingSupport(c.getEditingSupport());
		this.refreshConfig(c.getConfigRoot());
		this.bspSettingTool.setProperty(component, c.getType(), c.getProperties());
	}

	public Composite getContent() {
		return this.content;
	}

	public void viewReady() {

	}
}
